# -*- coding=utf8 -*-
__author__ = 'nickleo'

import shapeview, topicview, shape_utils, manager_utils
from django.http import HttpResponse, HttpResponseRedirect
import jsonpickle as json


def topic_all(request):
    return topicview.all(request)


# 列出cluster
def cluster_bid_mapping(request):
    list, alert = shape_utils.cluster_bid_mapping()
    return HttpResponse(json.encode(list))


# 列出topic
def topic_list(request):
    if request.GET.get('cluster') is None:
        name_server = 'Default name_server'
    list, alert = shape_utils.cluster_bid_mapping()
    return HttpResponse(json.encode(list))


def add_topic(request):
    result = {"statusCode": 200}
    alert = 'ok'
    namesvr=None
    if request.user.is_authenticated and request.user.has_perm('can_add_content_type'):
        cluster = None
        topic = None
        if request.GET.get('cluster') is None or request.GET.get('cluster').strip() == '':
            alert = 'cluster is null'
        else:
            cluster = request.GET.get('cluster')
        if request.GET.get('addtopic') is None or request.GET.get('addtopic').strip() == '':
            alert = 'addtopic is null'
        else:
            topic = request.GET.get('addtopic')

        if request.GET.get('namesvr') is None or request.GET.get('namesvr').strip() == '':
            alert = 'namesvr is null'
        else:
            namesvr = request.GET.get('namesvr')

        if (cluster is not None and topic is not None):
            statusCode, alert = manager_utils.add_topic(cluster, topic,namesvr)
            result["result"] = statusCode;

    else:
        alert = "User Not perm"

    result["alert"] = alert
    return HttpResponse(json.encode(result))


def del_topic(request):
    result = {"statusCode": 200}
    alert = 'ok'
    namesvr=None

    if request.user.is_authenticated and request.user.has_perm('can_del_content_type'):
        cluster = None
        topic = None
        if request.GET.get('cluster') is None or request.GET.get('cluster').strip() == '':
            alert = 'cluster is null'
        else:
            cluster = request.GET.get('cluster')
        if request.GET.get('topic') is None or request.GET.get('topic').strip() == '':
            alert = 'topic is null'
        else:
            topic = request.GET.get('topic')
        if request.GET.get('namesvr') is None or request.GET.get('namesvr').strip() == '':
            alert = 'namesvr is null'
        else:
            namesvr = request.GET.get('namesvr')
        if (cluster is not None and topic is not None):
            statusCode, alert = manager_utils.del_topic(cluster, topic,namesvr)
            result["result"] = statusCode;

    else:
        alert = "User Not perm"

    result["alert"] = alert
    return HttpResponse(json.encode(result))


def add_subgroup(request):
    result = {"statusCode": 200}
    alert = 'ok'
    namesvr=None
    if request.user.is_authenticated and request.user.has_perm('can_add_content_type'):
        cluster = None
        subgroup = None
        if request.GET.get('cluster') is None or request.GET.get('cluster').strip() == '':
            alert = 'cluster is null'
        else:
            cluster = request.GET.get('cluster')
        if request.GET.get('subgroup') is None or request.GET.get('subgroup').strip() == '':
            alert = 'subgroup is null'
        else:
            subgroup = request.GET.get('subgroup')
        if request.GET.get('namesvr') is None or request.GET.get('namesvr').strip() == '':
            alert = 'namesvr is null'
        else:
            namesvr = request.GET.get('namesvr')

        if (cluster is not None and subgroup is not None):
            statusCode, alert = manager_utils.add_subgroup(cluster, subgroup,namesvr)
            result["result"] = statusCode;

    else:
        alert = "User Not perm"

    result["alert"] = alert
    return HttpResponse(json.encode(result))


def del_subgroup(request):
    result = {"statusCode": 200}
    alert = 'ok'
    namesvr=None
    if request.user.is_authenticated and request.user.has_perm('can_del_content_type'):
        cluster = None
        subgroup = None
        if request.GET.get('cluster') is None or request.GET.get('cluster').strip() == '':
            alert = 'cluster is null'
        else:
            cluster = request.GET.get('cluster')
        if request.GET.get('subgroup') is None or request.GET.get('subgroup').strip() == '':
            alert = 'subgroup is null'
        else:
            topic = request.GET.get('subgroup')
        if request.GET.get('namesvr') is None or request.GET.get('namesvr').strip() == '':
            alert = 'namesvr is null'
        else:
            namesvr = request.GET.get('namesvr')
        if (cluster is not None and topic is not None):
            statusCode, alert = manager_utils.del_subgroup(cluster, topic,namesvr)
            result["result"] = statusCode;

    else:
        alert = "User Not perm"

    result["alert"] = alert
    return HttpResponse(json.encode(result))


def up_broker_cfg(request):
    result = {"statusCode": 200}
    alert = 'ok'
    # 'type': type,
    # 'namesvr': namesvr,
    # 'typeValue': typeValue,
    # 'key': key,
    # 'value': value

    if request.user.is_authenticated and request.user.has_perm('can_del_content_type'):
        type = None
        namesvr = None
        typeValue = None
        key = None
        value = None
        if request.GET.get('type') is None or request.GET.get('type').strip() == '':
            alert = 'type is null'
        else:
            type = request.GET.get('type')
        if request.GET.get('namesvr') is None or request.GET.get('namesvr').strip() == '':
            alert = 'namesvr is null'
        else:
            namesvr = request.GET.get('namesvr')
        if request.GET.get('typeValue') is None or request.GET.get('typeValue').strip() == '':
            alert = 'typeValue is null'
        else:
            typeValue = request.GET.get('typeValue')
        if request.GET.get('key') is None or request.GET.get('key').strip() == '':
            alert = 'key is null'
        else:
            key = request.GET.get('key')
        if request.GET.get('value') is None or request.GET.get('value').strip() == '':
            alert = 'value is null'
        else:
            value = request.GET.get('value')

        if type is not None and namesvr is not None and typeValue is not None and key is not None and value is not None:
            _statusCode, _alert = manager_utils.up_broker_cfg(type, namesvr, typeValue, key, value)
            result["result"] = _statusCode
            result["alert"] = _alert
        else:
            alert = "args is null "

    else:
        alert = "User Not perm"

    result["alert"] = alert
    return HttpResponse(json.encode(result))